#include "IndexBuffer.h"

IndexBuffer::IndexBuffer(Graphics& gfx, const std::vector<uint32_t>& indices)
	:
	IndexBuffer(gfx, "?", indices)
{}
IndexBuffer::IndexBuffer(Graphics& gfx, std::string tag, const std::vector<uint32_t>& indices)
	:
	tag(tag),
	count((UINT)indices.size())
{
	
	D3D11_BUFFER_DESC ibd = {};
	ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
	ibd.Usage = D3D11_USAGE_DEFAULT;
	ibd.CPUAccessFlags = 0u;
	ibd.MiscFlags = 0u;
	ibd.ByteWidth = UINT(count * sizeof(uint32_t));
	ibd.StructureByteStride = sizeof(uint32_t);
	D3D11_SUBRESOURCE_DATA isd = {};
	isd.pSysMem = indices.data();
	HR(GetDevice(gfx)->CreateBuffer(&ibd, &isd, &pIndexBuffer));
}

void IndexBuffer::Bind(Graphics& gfx) 
{
	GetContext(gfx)->IASetIndexBuffer(pIndexBuffer.Get(), DXGI_FORMAT_R32_UINT, 0u);
}

UINT IndexBuffer::GetCount() const noexcept
{
	return count;
}
std::shared_ptr<IndexBuffer> IndexBuffer::Create(Graphics& gfx, const std::string& tag,
	const std::vector<uint32_t>& indices)
{
	assert(tag != "?");
	return std::make_shared<IndexBuffer>(gfx, tag, indices);
}